package com.controller;

import com.entity.*;
import com.service.*;
import com.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ZixunController
 * @Description 新闻资讯模块控制层
 */
@Controller
public class ZixunController extends BaseController{

    @Autowired
    private ZixunService zixunService;




    //前台-新闻资讯列表页
    @RequestMapping("/zixunManaQian")
    public ModelAndView zixunManaQian(Zixun req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("zixunManaQian");
        mav.addObject("actionName","zixunManaQian");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-新闻资讯图片列表页
    @RequestMapping("/zixunManaQiantp")
    public ModelAndView zixunManaQiantp(Zixun req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("zixunManaQiantp");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //后台-新闻资讯管理列表页
    @RequestMapping("/zixunMana")
    public ModelAndView zixunMana(Zixun req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("zixunMana");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //后台-新闻资讯查看列表页
    @RequestMapping("/zixunChakan")
    public ModelAndView zixunChakan(Zixun req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("zixunChakan");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-新闻资讯按id列排序列表页
    @RequestMapping("/zixunManaQianSort")
    public ModelAndView zixunManaQianSort(Zixun  req, @RequestParam(defaultValue = "1", value = "pageNo") int pageNo) {
        ModelAndView mav = new ModelAndView("zixunManaQian");
        mav.addObject("actionName","zixunManaQianSort");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-新闻资讯详情页
    @GetMapping("/zixunDetailQian/{id}")
    @ResponseBody
    public ModelAndView zixunDetailQian(@PathVariable Integer id){
        ModelAndView mav = new ModelAndView("zixunDetailQian");
        Zixun info= zixunService.findById(id);
        mav.addObject("zixun", info);

        return mav;
    }

    //后台-新闻资讯详情页
    @GetMapping("/zixunDetail/{id}")
    @ResponseBody
    public ModelAndView zixunDetail(@PathVariable Integer id){
        ModelAndView mav = new ModelAndView("zixunDetail");
        Zixun info= zixunService.findById(id);
        mav.addObject("zixun", info);
        return mav;
    }

    //获取所有新闻资讯数据接口
    @RequestMapping("/zixunAll")
    @ResponseBody
    public ApiResponse<Zixun> zixunAll(){
        List<Zixun> zixunList=zixunService.findAll();
        return ApiResponse.success(zixunList);
    }

    //根据条件（字符类型模糊匹配查询）获取新闻资讯数据接口
    @RequestMapping("/zixunSearch")
    @ResponseBody
    public ApiResponse<Zixun> zixunSearch(Zixun req){
        List<Zixun> zixunList=zixunService.find(req);
        return ApiResponse.success(zixunList);
    }

    //根据条件（字符类型完全匹配查询）获取新闻资讯数据接口
    @RequestMapping("/zixunSearchByEqualTo")
    @ResponseBody
    public ApiResponse<Zixun> zixunSearchByEqualTo(Zixun req){
        List<Zixun> zixunList=zixunService.findByEqualTo(req);
        return ApiResponse.success(zixunList);
    }

    //新闻资讯添加或修改接口
    @PostMapping("/zixunAddOrUpdate")
    @ResponseBody
    public ApiResponse<Integer> zixunAddOrUpdate(@RequestBody Zixun req, HttpSession session, Boolean isUPdateSession){



        int result = zixunService.saveOrUpdate(req);


        return ApiResponse.success(result);
    }

    //新闻资讯修改单独一个字段接口
    @PostMapping("/zixunSet")
    @ResponseBody
    public ApiResponse<Integer> zixunSet(Zixun req){
        int result = zixunService.saveOrUpdate(req);
        return ApiResponse.success(result);
    }



    //新闻资讯删除接口
    @RequestMapping("/zixunDel/{id}")
    @ResponseBody
    public ApiResponse<Integer> zixunDel(@PathVariable Integer id){
        int result = zixunService.deleteById(id);
        return ApiResponse.success(result);
    }

    //后台-新闻资讯修改页
    @RequestMapping("/zixunGet/{id}")
    @ResponseBody
    public ModelAndView zixunGet(@PathVariable Integer id){
        ModelAndView mav = new ModelAndView("zixunSet");
        Zixun info = zixunService.findById(id);
        mav.addObject("zixun", info);
        return mav;
    }








    //新闻资讯模糊匹配查询分页数据封装
    private ModelAndView getModelAndView(Zixun req, int pageNo, ModelAndView mav, String sortProperty) {
        PageWrap<Zixun> pageWrap=new PageWrap<Zixun>();
        pageWrap.setModel(req);
        pageWrap.setPageNum(pageNo);
        //设置每页最多几条数据，默认为8条
        //pageWrap.setPageSize(8);
        PageWrap.SortData sortData=new PageWrap.SortData();
        sortData.setDirection("DESC");
        sortData.setProperty(sortProperty);
        List<PageWrap.SortData> sorts = new ArrayList<>();
        sorts.add(sortData);
        pageWrap.setSorts(sorts);
        PageData<Zixun> pageInfo= zixunService.findPage(pageWrap);
        mav.addObject("pageInfo", pageInfo);


        mav.addObject("req", req);

        return mav;
    }

    //新闻资讯完全匹配查询分页数据封装
    private ModelAndView getModelAndViewByEqualTo(Zixun req, int pageNo, ModelAndView mav, String sortProperty) {
        PageWrap<Zixun> pageWrap=new PageWrap<Zixun>();
        pageWrap.setModel(req);
        pageWrap.setPageNum(pageNo);
        //设置每页最多几条数据，默认为8条
        //pageWrap.setPageSize(8);
        PageWrap.SortData sortData=new PageWrap.SortData();
        sortData.setDirection("DESC");
        sortData.setProperty(sortProperty);
        List<PageWrap.SortData> sorts = new ArrayList<>();
        sorts.add(sortData);
        pageWrap.setSorts(sorts);
        PageData<Zixun> pageInfo= zixunService.findPageByEqualTo(pageWrap);
        mav.addObject("pageInfo", pageInfo);


        mav.addObject("req", req);

        return mav;
        }
}
